package com.rs.service.impl;

import com.rs.config.CurrentUser;
import com.rs.dao.ClassDao;
import com.rs.dao.RoleDao;
import com.rs.entity.Class;
import com.rs.entity.Student;
import com.rs.entity.login.Menu;
import com.rs.entity.login.Role;
import com.rs.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassDao classDao;
    @Autowired
    private RoleDao roleDao;


    @Override
    public List<Class> getClassList(long page, long limit, String name){

        return classDao.getClassList((page-1)*limit,limit,name, CurrentUser.getUserId());
    }

    @Override
    public Long getClassSum(String name){

        return classDao.getClassSum(name,CurrentUser.getUserId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addClass(String name, String remark,Integer id){
        return classDao.addClass(name,remark,id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClass(long id,String name,String remark){
        classDao.updateClass(id,name,remark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delClass(long id){
        classDao.deleteClass(id);
    }


    @Override
    public List<Menu> getOneMenu(){

        return roleDao.getOneMenu();
    }


    @Override
    public List<Menu> getTwoMenu(List<Menu> haveMenus){
        List<Menu> twoMenu=roleDao.getTwoMenu();
        for (Menu two:twoMenu){
            for (Menu have:haveMenus){
                if(two.getId()==have.getId()){
                    two.setChecked(true);
                    break;
                }
            }
        }
        return twoMenu;
    }


    @Override
    public List<Student> getHaveStudent(long id){

        return classDao.getHaveStudent(id);
    }

    @Override
    public List<Student> getStudent(){
        return classDao.getStudent();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void allot(String mid,int id){
        String[] ids=mid.split(",");
        //删除该角色已有权限
        classDao.deletePower(id);
        //批量插入勾选的权限
        classDao.addPower(ids,id);
    }


    @Override
    public List<Role> getAll(){

        return roleDao.getAll();
    }


    @Override
    public List<Class> getClassByTeaId(Integer id){


        return classDao.getClassByTeaId(id);
    }


    @Override
    public List<Class> getUserClassList(int id){

        return classDao.getUserClassList(id);
    }

    @Override
    public Class getOne(int id) {

        return classDao.selectById(id);
    }
}
